package com.example.tests;

import com.example.data.User;
import com.example.fw.AccountHelper;
import com.example.fw.JamesHelper;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.testng.Assert.assertTrue;

/**
 * Created with IntelliJ IDEA.
 * User: raimbek
 * Date: 21.04.13
 * Time: 21:19
 */
public class SignupTest extends TestBase {
    JamesHelper james;
    AccountHelper accHelper;

    User user = new User().setLogin("testuser2").setPassword("123456")
            .setEmail("testuser2@localhost.localdomain");

    @BeforeClass
    public void createMailUser() {
        james = app.getJamesHelper();
        accHelper = app.getAccountHelper();
        if (!james.doesUserExist(user)) {
            james.createUser(user);
        }
    }

    @Test
    public void newUserShouldSignup() {
        accHelper.signup(user);
        accHelper.login(user);
        assertThat(accHelper.loggedUser(), equalTo(user.login));
    }

    //@Test
    public void existingUserShouldNotSignup() {
        try {
            accHelper.signup(user);
        } catch (Exception e) {
            assertThat(e.getMessage(), containsString("That username is already being used"));
        }
        fail("Exception expected");
    }

    @AfterClass
    public void deleteMailUser() {
        if (james.doesUserExist(user)) {
            james.deleteUser(user);
        }
    }
}